package com.qf.service.impl;

import com.qf.dao.*;
import com.qf.pojo.entity.Goods;
import com.qf.pojo.entity.StoreInfo;
import com.qf.pojo.entity.UserAssess;
import com.qf.pojo.entity.Users;
import com.qf.pojo.req.UserAssessReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.UserAssessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName UserAssessServiceImpl
 * @Description TODO
 * @Author 16222
 * @Date 2022/9/29 14:05
 * @Version 1.0
 **/
@Service
public class UserAssessServiceImpl implements UserAssessService  {
    @Autowired
    UserAssessRepository userAssessRepository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    TakeOutStoreInfoRepository takeOutStoreInfoRepository ;
    @Autowired
    TakeOutGoodsRepository takeOutGoodsRepository ;


    @Override
    public BaseResp findAllByStoreIdAndGoodsId(int storeId, int goodsId) {
        List<UserAssess> allByStoreIdAndGoodsId = userAssessRepository.findAllByStoreIdAndGoodsId(storeId, goodsId);

        return new BaseResp(200,"success",allByStoreIdAndGoodsId);
    }

    @Override
    public BaseResp findAllByStoreId(int storeId) {
        List<UserAssess> allByStoreId = userAssessRepository.findAllByStoreId(storeId);
        return new BaseResp(200,"success",allByStoreId);
    }

    @Override
    public BaseResp findAllByUserId(int userId) {
        List<UserAssess> allByUserId = userAssessRepository.findAllByUserId(userId);
        return new BaseResp(200,"success",allByUserId);
    }

    @Override
    public BaseResp saveAndFlush(UserAssessReq userAssessReq) {
        Integer userId = userAssessReq.getUserId();
        Integer storeId = userAssessReq.getStoreId();
        Integer goodsId = userAssessReq.getGoodsId();

        Users users = userRepository.findById(userId).get();
        StoreInfo storeInfo = takeOutStoreInfoRepository.findById(storeId).get();
        Goods goods = takeOutGoodsRepository.findById(goodsId).get();

        UserAssess userAssess = new UserAssess();
        userAssess.setAssessCreatetime(new Date());
        userAssess.setAssessMessage(userAssessReq.getAssessMessage());

        userAssess.setStoreId(storeInfo.getId());
        userAssess.setStoreName(storeInfo.getStoreName());
        userAssess.setStorePic(storeInfo.getStorePic());
        userAssess.setUserId(users.getId());
        userAssess.setUserName(users.getUserName());
        userAssess.setUserPic(users.getUserPic());
        userAssess.setGoodsId(goods.getId());

        UserAssess userAssess1 = userAssessRepository.saveAndFlush(userAssess);
        System.out.println("=================添加的评论========"+userAssess1.toString());
        return new BaseResp().SUCCESS();
    }

}
